package snailAI.Utils;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Random;

// 指定加载邮件配置文件

@PropertySource(value = "classpath:emailservice.properties", encoding = "UTF-8")
@Component
public class EmailUtils {
    @Autowired
    private JavaMailSender mailSender;//别管，问就是能用

    private static final String BossMail="3181725457@qq.com";//咨询人员电话或者邮箱

    // 用于读取配置文件中的属性
    @Autowired
    private Environment env;

    /**
     * 发送验证码到指定邮箱
     * @param toEmail 接收验证码的邮箱地址
     * @return 发送是否成功
     */
    public String sendVerifyCode(String toEmail) {
        try {
            // 生成5位随机验证码
            String verifyCode = generateVerifyCode(5);

            // 创建邮件消息
            SimpleMailMessage message = new SimpleMailMessage();
            // 从emailservice.properties中获取发件人邮箱
            message.setFrom(env.getProperty("spring.mail.username"));
            message.setTo(toEmail);
            message.setSubject("【蜗牛租车】验证码");
            message.setText("您的验证码是：" + verifyCode + "，该验证码5分钟内有效，请尽快使用。");

            // 发送邮件
            mailSender.send(message);
            return verifyCode;
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败";
        }
    }

    /**
     * 生成指定长度的随机数字验证码
     */
    private String generateVerifyCode(int length) {
        Random random = new Random();
        StringBuilder codeBuilder = new StringBuilder();

        for (int i = 0; i < length; i++) {
            codeBuilder.append(random.nextInt(10));
        }

        return codeBuilder.toString();
    }
    public void sendNormalMsg(String msg,String toEmail){

        SimpleMailMessage message = new SimpleMailMessage();
        // 从emailservice.properties中获取发件人邮箱
        message.setFrom(env.getProperty("spring.mail.username"));
        message.setTo(toEmail);
        message.setSubject("新的咨询者");
        message.setText(msg);

        // 发送邮件
        mailSender.send(message);
    }



//    public String sendUserPhone(@ToolParam(description = "对话记录，由AI总结，只拿取Content属性") String record,
//    @ToolParam(description = "客户的电话号") String phone
//    ){
//
//        SimpleMailMessage message = new SimpleMailMessage(
//                );
//        message.setFrom(env.getProperty("spring.mail.username"));
//        message.setTo(BossMail);
//        message.setSubject("蜗牛消息");
//        message.setText("新的咨询人，大体情况如下：\n"+
//                record+"\n电话如下"+phone);
//        mailSender.send(message);
//        System.out.println(record+phone+"发送了");
//        return "好的，已经将相关消息发送到咨询老师了，老师正在看，过会老师会给您打电话的。";
//    }



}
